package code.cando;

import com.sun.org.apache.regexp.internal.RE;

import java.io.Serializable;
import java.util.Arrays;

/**
 * author : yeswater
 * date : 2023/12/7
 * <p>
 * 搜索旋转排序数组
 * <p>
 * 0751 - 0842
 */

public class T033 {
    public static void main(String[] args) {
        int[] nums = new int[]{4,5,6,7,0,1,2};
        int target = 2;
        System.out.println(search2(nums, target));
    }

    /**
     * 不正确的思路
     */
    public static int search1(int[] nums, int target) {
        int length = nums.length;
        int m = length >> 1;
        if (target < nums[0] && target > nums[length - 1]) {
            return -1;
        } else if (target < nums[0]) {
            return search(nums, target, m, length - 1);
        } else {
            return search(nums, target, 0, length - 1);
        }
    }

    public static int search(int[] nums, int target) {
        if (nums.length == 0) {
            return -1;
        }
        if (nums.length == 1) {
            if (nums[0] == target) {
                return 0;
            }
            return -1;
        }
        int minIndex = getMinIndex(nums);
        if (minIndex == 0) {
            return search(nums, target, 0, nums.length - 1);
        } else if (nums[0] <= target && nums[minIndex - 1] >= target) {
            return search(nums, target, 0, minIndex - 1);
        } else {
            return search(nums, target, minIndex, nums.length - 1);
        }

    }

    /**
     * 优秀思路
     */
    public static int search2(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        int mid = 0;
        while (l <= r) {  // 二分 这里记得加等号!!!
            mid = (l + r) >> 1;
            if (nums[mid] == target) {
                return mid;
            }
            
            // 判断 mid 是在左段还是右段
            if (nums[mid] >= nums[l]) { // 左段
                // 判断 target 与 mid 的位置关系
                if (target >= nums[l] && target < nums[mid] ) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            } else { // 右段
                if (target > nums[mid] && target <= nums[r]) {
                    l = mid + 1;
                } else {
                    r = mid - 1;
                }
            }
        }
        return -1;
    }

    // 二分查找
    public static int search(int[] nums, int target, int L, int R) {
        while (L < R) {
            int M = (L + R) >> 1;
            if (nums[M] > target) {
                R = M - 1;
            } else if (nums[M] < target) {
                L = M + 1;
            } else {
                return M;
            }
        }
        return nums[L] == target ? L : -1;
    }

    public static int getMinIndex(int[] nums) {
        int minIndex = 0;
        int minVal = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < minVal) {
                minVal = nums[i];
                minIndex = i;
            }
        }
        return minIndex;
    }

}
